#include "blockqueue.hpp"
#include "Task.hpp"

void* Consumer(void* arg)
{
    Blockqueue<Task>* bq = static_cast<Blockqueue<Task>*>(arg);
    while(true)
    {
        Task t = bq->pop();
        t.run();
        std::cout << "消费了一个任务：" << t.GetResult() << std::endl;
    }
}  

void* Producer(void* arg)
{
    int len = opers.size();
    Blockqueue<Task>* bq = static_cast<Blockqueue<Task>*>(arg);
    while(true)
    {
        int data1 = rand() % 10 + 1;
        int data2 = rand() % 10;
        char op = opers[rand() % len];
        Task t(data1, data2, op);
        bq->push(t);
        std::cout << "生产了一个任务：" << t.GetTask() << std::endl;
        sleep(1);
    }
}

int main()
{
    Blockqueue<Task>* bq = new Blockqueue<Task>();
    pthread_t c[3], p[5];
    for(int i = 0; i < 3; ++i)
    {
        pthread_create(&c[i], nullptr, Consumer, bq);
    }

    for(int i = 0; i < 5; ++i)
    {
        pthread_create(&p[i], nullptr, Producer, bq);
    }

    for(int i = 0; i < 3; ++i)
    {
        pthread_join(c[i], nullptr);
    }                   
    for(int i = 0; i < 5; ++i)
    {
        pthread_join(p[i], nullptr);    
    }
    delete bq;
    return 0;
}